/*
 * This code is licensed under the Apache License 2.0.
 * Read more at http://blippit.googlecode.com/
 */
package com.googlecode.blippit;

import org.apache.http.auth.AuthenticationException;

import android.os.Bundle;
import android.util.Log;


public class AsyncBlipApi {
	private static final String TAG = "blip";

	interface ResultCallback {
		void onComplete(Bundle result);
	}

	interface ApiCall {
		BlipApi.ApiResult run() throws AuthenticationException;
	}

	public static void startHello(final String query, ResultCallback owner) {
		ApiCall a = new ApiCall() {
			public BlipApi.ApiResult run() {
				return BlipApi.UtilService.hello();
			}
		};
		new ApiThread("UtilService.hello", a, owner).start();
	}

	public static void startSearch(final String query, ResultCallback owner) {
		ApiCall a = new ApiCall() {
			public BlipApi.ApiResult run() {
				return BlipApi.SearchService.findSongs(query);
			}
		};
		new ApiThread("SearchService.findSongs", a, owner).start();
	}

	public static void startGetPublic(final Integer offset, final Integer count,
			ResultCallback owner) {
		ApiCall a = new ApiCall() {
			public BlipApi.ApiResult run() {
				return BlipApi.BlipService.getPublic(offset, count);
			}
		};
		new ApiThread("BlipService.getPublic", a, owner).start();
	}

	public static void startGetUserProfile(final String username,
			final Integer offset, final Integer count, ResultCallback owner) {
		ApiCall a = new ApiCall() {
			public BlipApi.ApiResult run() throws AuthenticationException {
				return BlipApi.BlipService.getUserProfile(username, offset, count);
			}
		};
		new ApiThread("BlipService.getUserProfile", a, owner).start();
	}

	public static void startGetUserHome(final Integer offset,
			final Integer count, ResultCallback owner) {
		ApiCall a = new ApiCall() {
			public BlipApi.ApiResult run() throws AuthenticationException {
				return BlipApi.BlipService.getUserHome(offset, count);
			}
		};
		new ApiThread("BlipService.getUserHome", a, owner).start();
	}

	public static void startGetUserPlayList(final String username,
			ResultCallback owner) {
		ApiCall a = new ApiCall() {
			public BlipApi.ApiResult run() throws AuthenticationException {
				return BlipApi.BlipService.getUserPlaylist(username);
			}
		};
		new ApiThread("BlipService.getUserPlayList", a, owner).start();
	}

	private static class ApiThread extends Thread {
		private final ApiCall call;
		private final ResultCallback owner;

		ApiThread(String name, ApiCall call, ResultCallback owner) {
			super("BlipApi:" + name);
			this.call = call;
			this.owner = owner;
		}

		@Override
		public void run() {
			Bundle bundle = new Bundle();
			try {
				BlipApi.ApiResult result = call.run();
				if (result != null) {
					bundle.putInt("code", result.status_code);
					bundle.putString("status", result.status_message);
					bundle.putBundle("json", result.bundle);
				} else {
					Log.e(TAG, "Result: null");
				}
			} catch (Exception e) {
				bundle.putString("exception", e.getClass().getName());
				bundle.putString("message", e.getMessage());
			} finally {
				owner.onComplete(bundle);
			}
		}
	}
}
